//
//  NewsViewController.swift
//  lookLikeIweekly
//
//  Created by ZhangXiaoqian on 16/6/10.
//  Copyright © 2016年 Mainzxq. All rights reserved.
//

import UIKit

class NewsViewController: UIViewController {
    
    private var myScroll: NewsScrollView = NewsScrollView()
    var newsArray: Array = [PictureArticleSection]()
    var queque = NSOperationQueue()
    var menuController: MenuCollectionController?
    let userData = NSUserDefaults.standardUserDefaults()
    var mainNewsCollectionController: MainNewsCollectionController?
    override func viewDidLoad() {
        super.viewDidLoad()

        queque.maxConcurrentOperationCount = 3
        menuController = MenuCollectionController(collectionViewLayout: UICollectionViewLayout())
        menuController!.collectionView = NewsCollectionView(frame: CGRectMake(0,0,self.view.bounds.size.width, 25), collectionViewLayout: UICollectionViewLayout())
        menuController?.delegate = self
        addChildViewController(menuController!)
        menuController!.view!.frame = CGRectMake(0, 60, self.view.bounds.size.width, 25)
        
        self.view.addSubview(menuController!.view!)
        menuController!.viewDidLoad()
        menuController!.didMoveToParentViewController(self)
        
        print("menu loaded")
        let rands = arc4random() % 16
        print(rands)
        if userData.objectForKey(titleArray[Int(rands)].title) != nil {
            let temp = userData.objectForKey(titleArray[Int(rands)].title) as? NSData
            self.newsArray = NSKeyedUnarchiver.unarchiveObjectWithData(temp!) as! [PictureArticleSection]
        } else {
        self.newsArray = workNewsOut(titleArray[Int(rands)].1)
        let block1 = NSBlockOperation { () -> Void in
            for item in self.newsArray {
                item.getImage()
                print("在执行循环图片")
            }

        }
            let block2 = NSBlockOperation { () -> Void in
                print(self.newsArray[0].albumImage)
                let res: NSData = NSKeyedArchiver.archivedDataWithRootObject(self.newsArray)
                self.userData.setObject(res, forKey: titleArray[16].title)
                self.userData.synchronize()
                print("已完成存储")
            }
            block2.addDependency(block1)
            queque.addOperation(block1)
            queque.addOperation(block2)

        }


        
        print("scroll loaded")
        
        mainNewsCollectionController = MainNewsCollectionController(collectionViewLayout: UICollectionViewLayout())
        
        mainNewsCollectionController!.collectionView = NewsCollectionView(frame: CGRectMake(0, 285, WIDTH, HEIGHT-285),collectionViewLayout: UICollectionViewLayout())
        mainNewsCollectionController!.newsArray = newsArray
        addChildViewController(mainNewsCollectionController!)
        mainNewsCollectionController!.view.frame = CGRectMake(0, 285, WIDTH, HEIGHT-285)
        self.view.addSubview(mainNewsCollectionController!.view)
        
        mainNewsCollectionController!.viewDidLoad()
        mainNewsCollectionController!.didMoveToParentViewController(self)
        
        print("Main News Loaded")
        
        
        menuController!.callBack{(num: Int) in
            print("\(num)号新闻被点击")
            // self.myScroll.removeFromSuperview()
            self.newsArray = workNewsOut(titleArray[num].1)
            let num1 = self.newsArray.count
            
            if self.userData.objectForKey(titleArray[num].0) != nil {
                let temp = self.userData.objectForKey(titleArray[num].0) as! NSData
                self.newsArray = NSKeyedUnarchiver.unarchiveObjectWithData(temp) as! [PictureArticleSection]
            } else {
            
            let block3 = NSBlockOperation { () -> Void in
                
                for item in self.newsArray {
                    item.getImage()
                    print("在执行循环图片")
                }
            }
            let block4 = NSBlockOperation { () -> Void in
                    let res: NSData = NSKeyedArchiver.archivedDataWithRootObject(self.newsArray)
                    self.userData.setObject(res, forKey: titleArray[num].title)
                    self.userData.synchronize()
                    print("储存完毕")
                }
                block4.addDependency(block3)
                self.queque.addOperation(block3)
                self.queque.addOperation(block4)
            }
            
            self.mainNewsCollectionController!.newsArray = self.newsArray
            self.mainNewsCollectionController!.collectionView?.collectionViewLayout = OneColumnLayOut(count: num1)
            self.mainNewsCollectionController!.collectionView?.reloadData()

        }

    }


    override func viewDidAppear(animated: Bool) {
        super.viewWillAppear(true)
        // 考虑实现滑动新闻时自动收起
        myScroll = NewsScrollView(frame: CGRect(x: 0, y: 85, width: view.frame.size.width, height: 200))
        myScroll.newsList = self.newsArray
        myScroll.tag = 1032
        myScroll.setUpSubViews()
        view.addSubview(myScroll)
  
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    


    @IBAction func quitBtn(sender: AnyObject) {
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    // 实现协议方法

}

extension NewsViewController: sendDataProtocl {
    
    func tapBarBtn(barIndex: Int) {
        print("\(barIndex)被点击")
    }
}
